package com.yisu.expressway.login;


import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.yisu.expressway.application.ExApplication;
import com.yisu.expressway.model.UserProfile;
import com.yisu.expressway.settings.AppConfig;
import com.yisu.expressway.settings.AppGlobalSetting;
import com.yisu.expressway.userprofile_setting.UpdateProfileEvent;
import com.yisu.expressway.utils.StringUtil;
import com.youzan.sdk.YouzanSDK;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Administrator on 2016/9/13 0013.
 */
public class LoginManager {

    private static boolean sIsLoggedIn;

    private static String sLongToken;
    private static String sUserId;

    private static UserProfile user;

    private static AppGlobalSetting sp;
    private static Context mContext;
    private static String sGetuiPushId;

    public static void onLoginSuccess( UserProfile _user) {
        if(_user.accountType == 0 && user != null && user.accountType > 0 ) {
            //在修改资料或获取用户详情的时候,记住第三方登录类型
            _user.accountType = user.accountType;
        }

        user = _user;
        setLoggedIn(true);
        setUserId(user.userAppId);
        setLongToken(user.token);
        sp.setStringGlobalItem(AppConfig.PREF_USER, new Gson().toJson(user));
    }

    public static void onLogout() {
        setLoggedIn(false);
        sp.removeKey(AppConfig.PREF_USER);
        sp.removeKey(AppConfig.PREF_LOGIN_LONG_TOKEN);
        user = null;
        YouzanSDK.userLogout(ExApplication.getInstance().getApplicationContext());
        EventBus.getDefault().post(new UpdateProfileEvent(true));
    }

    public static boolean isLoggedIn() {
        return sIsLoggedIn;
    }

    public static void setLoggedIn(boolean value) {
        sIsLoggedIn = value;
        final AppGlobalSetting sp = new AppGlobalSetting(ExApplication.getInstance().getApplicationContext());
        sp.setBooleanGlobalItem(AppConfig.PREF_USER_IS_LOGIN, sIsLoggedIn);
    }

    public static String getLongToken() {
        return sLongToken;
    }

    public static void setLongToken(String token) {
        if (user != null)
            user.token = token;
        sLongToken = token;
        final AppGlobalSetting sp = new AppGlobalSetting(ExApplication.getInstance().getApplicationContext());
        sp.setStringGlobalItem(AppConfig.PREF_LOGIN_LONG_TOKEN, sLongToken);
    }

    public static String getUserId() {
        return sUserId;
    }

    public static void setUserId(String id) {
        sUserId = id;
        final AppGlobalSetting sp = new AppGlobalSetting(ExApplication.getInstance().getApplicationContext());
        sp.setStringGlobalItem(AppConfig.PREF_LOGIN_USER_ID, sUserId);
    }

    public static void initialize(Context context) {
        sp = new AppGlobalSetting(context);
        mContext = context;
        sIsLoggedIn = sp.getBooleanGlobalItem(AppConfig.PREF_USER_IS_LOGIN, false);
        sLongToken = sp.getStringGlobalItem(AppConfig.PREF_LOGIN_LONG_TOKEN, "");
        sUserId = sp.getStringGlobalItem(AppConfig.PREF_LOGIN_USER_ID, "");
        sGetuiPushId = sp.getStringGlobalItem(AppConfig.PREF_PUSH_CLIENT_ID,"");
        String userSp = sp.getStringGlobalItem(AppConfig.PREF_USER, null);

        if (userSp != null){
            user = new Gson().fromJson(userSp, UserProfile.class);
        }
    }

    public static UserProfile getUser(){
        return user;
    }

    public static String getUserAccount(){
        return user.userAccount;
    }

    public static void setPushClientId(String clientId){
        sGetuiPushId = clientId;
        if(sp != null){
            sp.setStringGlobalItem(AppConfig.PREF_PUSH_CLIENT_ID,clientId);
        }

    }

    public static String getPushClientId(){
        if(StringUtil.isNullOrEmpty(sGetuiPushId)){
            if(sp != null){
                sp.getStringGlobalItem(AppConfig.PREF_PUSH_CLIENT_ID,"");
            }
        }
        return sGetuiPushId;
    }

}
